package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.*;
import com.example.common.util.R;
import com.example.buildtable.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "商品")
@RequestMapping(value = "/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    
    @Operation(summary = "查询所有商品数据")
    @GetMapping(value = "/getAllGoods")
    public R getAllGoods() {
        LambdaJoinQueryWrapper<Goods> wrapper = new LambdaJoinQueryWrapper<>(Goods.class);
        wrapper.innerJoin(ProductInbound.class, ProductInbound::getProductInboundId, Goods::getGoodsInboundId)
                .innerJoin(Product.class, Product::getProductId, Goods::getGoodsProductId)
                .innerJoin(ProductSubcategory.class, ProductSubcategory::getProductSubcategoryId, Goods::getGoodsProductSubcategoryId);
        List<Goods> goodsList = goodsService.list(wrapper);
        return R.ok("查询所有商品数据成功").data("goodsList", goodsList);
    }
    
    @Operation(summary = "增加商品")
    @PostMapping(value = "/addGoods")
    public R addGoods(Goods goods) {
        boolean flag = goodsService.save(goods);
        if (flag) {
            return R.ok("增加商品成功");
        } else {
            return R.ok("增加商品失败");
        }
    }

    @Operation(summary = "删除商品")
    @GetMapping(value = "/deleteGoods/{goodsId}")
    public R deleteGoods(@PathVariable("goodsId") Integer goodsId) {
        boolean flag = goodsService.removeById(goodsId);
        if (flag) {
            return R.ok("删除商品成功");
        } else {
            return R.ok("删除商品失败");
        }
    }

    @Operation(summary = "按照商品查看")
    @GetMapping(value = "/getGoodsById/{goodsId}")
    public R getGoodsById(@PathVariable("goodsId") Integer goodsId) {
        LambdaJoinQueryWrapper<Goods> wrapper = new LambdaJoinQueryWrapper<>(Goods.class);
        wrapper.innerJoin(ProductInbound.class, ProductInbound::getProductInboundId, Goods::getGoodsInboundId)
                .innerJoin(Product.class, Product::getProductId, Goods::getGoodsProductId)
                .innerJoin(ProductSubcategory.class, ProductSubcategory::getProductSubcategoryId, Goods::getGoodsProductSubcategoryId);
        wrapper.eq(Goods::getGoodsId, goodsId);
        Goods goods = goodsService.getOne(wrapper);
        if (goods != null) {
            return R.ok("按照商品查看成功").data("goods", goods);
        } else {
            return R.error("按照商品查看失败");
        }
    }

    @Operation(summary = "修改商品")
    @PostMapping(value = "/updateGoods")
    public R updateGoods(Goods goods) {
        boolean flag = goodsService.updateById(goods);
        if (flag) {
            return R.ok("修改商品成功");
        } else {
            return R.ok("修改商品失败");
        }
    }
}